//
//  VisionCenter.swift
//  App_Vision
//
//  Created by Flutter on 2021/7/28.
//

import Foundation
import Standard
import Storage
import Center_Storage


enum VisionCenter {}

extension VisionCenter: TypeName{}

struct VisionInfo: CodableStorable {
    var defaultDistance: Float?
    var defaultVisionIndex: Int?
    var defaultGlass: Bool?
}


private extension VisionCenter {
    static let logger = Loggers[typeName]
    static let store = StorageCenter.coke[typeName]
}

extension VisionCenter {
    static var visionInfo: VisionInfo = {
        do {
            guard let visionInfo: VisionInfo = try store.sync.get() else { return VisionInfo() }
            return visionInfo
        } catch {
            logger.error("\(error)")
            return VisionInfo()
        }
    }() {
        didSet {
            do {
                try store.sync.put(storableObject: visionInfo)
            } catch {
                logger.error("\(error)")
            }
        }
    }
}

extension VisionCenter {
    static func clearVisionInfo() {
        visionInfo = VisionInfo()
    }
}

